package in.muton.edushare.pages.publicpages.univpages;

import in.muton.edushare.EduSharePage;
import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareLogin;
import in.muton.edushare.pages.FooterLinks;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UniversitiesDAO;
import com.edushare.common.exceptions.EduShareRuntimeException;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Universities;

public abstract class UnivBasePublic extends EduSharePage {
	public UnivBasePublic(PageParameters params) {
		super();
		getUniversity(params);
		add(new Label("pageTitle", univModel.getObject().getUniversityName()));
		add(new Label("univName", univModel.getObject().getUniversityName()));
		addLinks(params);
		add(CSSPackageResource.getHeaderContribution(UnivBasePublic.class, "PublicPage.css"));
		add(CSSPackageResource.getHeaderContribution(UnivBasePublic.class, "Structure.css"));
		add(new FooterLinks("footerLinks"));
	}

	public void getUniversity(PageParameters params) {
		String univId = params.getString("0");
		if (StringUtil.isValid(univId)) {
			UniversitiesDAO uDAO = new UniversitiesDAO(getSessionORMInterface());
			Universities univ = uDAO.getUnivById(univId);
			if (univ != null) {
				univModel = new CayenneModel<Universities>(univ);
				return;
			}
		}
		throw new EduShareRuntimeException("Unable to find univ by id");
	}

	public void addLinks(PageParameters params) {
		for (String p : pageNames) {
			Link<Void> l = null;
			if (p.equals("univPublic")) {
				add(l = new BookmarkablePageLink<Void>("univPublic",
						UnivPublic.class, params));
			} else if (p.equals("univPublicDept")) {
				add(l = new BookmarkablePageLink<Void>("univPublicDept",
						UnivPublicDept.class, params));
			} else if (p.equals("univPublicCourse")) {
				add(l = new BookmarkablePageLink<Void>("univPublicCourse",
						UnivPublicCourse.class, params));
			} else if (p.equals("univPublicFaculty")) {
				PageParameters params2 = new PageParameters();
				params2.put("0", getUnivModel().getObject().getUniqueName());
				params2.put("1", "faculty");
				add(l = new BookmarkablePageLink<Void>("univPublicFaculty",
						UnivPublicUsers.class, params2));
			} else if (p.equals("univPublicStudent")) {
				PageParameters params2 = new PageParameters();
				params2.put("0", getUnivModel().getObject().getUniqueName());
				params2.put("1", "students");
				add(l = new BookmarkablePageLink<Void>("univPublicStudent",
						UnivPublicUsers.class, params2));
			} else {
				if(EduShareSession.get().isLoggedIn()){
					add(l=new BookmarkablePageLink<Void>("login", URLLoader.getPageClass(URLConstants.USER_PROFILE_PATH)));
					l.add(new Label("myHome", "My Home"));
				}else{
					add(l = new BookmarkablePageLink<Void>("login",EduShareLogin.class));
					l.add(new Label("myHome", "Login"));
				}
			}
			if (p.equals(getPageName(params))) {
				l.add(new SimpleAttributeModifier("class", "pubMenuSel"));
			}
		}
	}

	public abstract String getPageName(PageParameters params);

	public static final String pageNames[] = { "univPublic", "univPublicDept",
			"univPublicCourse", "univPublicFaculty", "univPublicStudent",
			"univPublicProgram" };

	private IModel<Universities> univModel = null;

	public IModel<Universities> getUnivModel() {
		return univModel;
	}
}
